<!--
//
// Trigger.vue
// vue3-element-admin
//
// Created by 区区电脑 on 2023/04/11.
//
-->
<template>
    <div class="ElemList">
        <!-- 筛选区 -->
        <table-search-bar :config="requestBodyConfig" :model="requestBody" hide-search style="height: 100px" title="标样元素" @search="searchClicked" />
        <div class="container">
            <!-- 功能按钮 -->
            <div class="table-buttons">
                <el-button v-permission="permission.add" type="primary" @click="addData">
                    <b-icon name="bi-plus" size="20" />
                    添加
                </el-button>
            </div>
            <!--checkbox设置 :checkbox-config="{trigger: 'row', highlight: true}" -->
            <!--radio设置 :radio-config="{trigger: 'row', highlight: true}" -->
            <vxe-table ref="VXETable" :column-config="{ resizable: false}" :data="state.list" :height="tableHeight" :loading="state.isLoading" :radio-config="{trigger: 'row', highlight: true}" :row-config="{isCurrent: true, isHover: true, keyField: 'id'}" :tree-config="{childrenField: 'children', reserve: true}" border round show-overflow size="small" @current-change="() => {}" @radio-change="() => {}">
                <template #empty>
                    <el-empty :image-size="100" />
                </template>
                <vxe-column fixed="left" title="#" type="seq" width="50" />
                <vxe-column align="left" field="elemSymbol" min-width="100" title="元素名称"></vxe-column>
                <vxe-column align="left" field="concentration" min-width="100" title="元素含量"></vxe-column>
                <vxe-column :resizable="false" :width="$widthColumn(100)" field="action" fixed="right" title="操作">
                    <template #default="{row}">
                        <table-action :actions="[
                            {title: '修改', permission: permission.edit, type: 'primary', ok: editData},
                            {title: '删除', permission: permission.delete, type: 'danger', ok: deleteData, confirm: '您确定要删除当前数据么?'},
                        ]" :data="row" />
                    </template>
                </vxe-column>
            </vxe-table>
        </div>
    </div>
    <EditForm ref="editForm" />
</template>

<script setup>
import {ref, reactive, computed, watch, onMounted} from "vue";
import {useStore} from "vuex";
import {useRouter} from "vue-router";
import * as api from "./api.js"
import EditForm from "./EditForm.vue"

const {$message, $widthColumn} = window;

const emits = defineEmits();

// 路由
const router = useRouter();
// store
const store = useStore();
const tableHeight = computed(() => store.getters.layoutType === "mobile" ? "" : store.getters.containerHeight - 195)
// 响应式数据
const state = reactive({
    // 表格数据加载中
    isLoading: false,
    // 表格数据
    list: []
})

// 当前页面功能权限
const permission = {
    add: "StdSample:AddStdSampleConc",
    delete: "StdSample:UpdateStdSampleConc",
    edit: "StdSample:UpdateStdSampleConc",
};

// 请求body参数
const requestBody = reactive({
    elemSymbol: null,
})
// 筛选区域配置
const requestBodyConfig = reactive({
    elemSymbol: {label: "元素名称", component: "el-input"},
})

const editForm = ref();

// 添加
function addData() {
    editForm.value.show({
        title: "添加",
        options: {stdSampleId: state.stdSampleId},
        ok: () => {
            loadList(true);
        }
    })
}

// 删除
async function deleteData({id}) {
    console.danger("删除: ", id)
    const {success, data} = await api.DeleteStdSampleConc({id});
    if (!success) return 0;
    loadList();
    $message.success("删除成功")
}

// 修改
function editData(row) {
    editForm.value.show({
        title: "修改",
        options: {...row},
        ok: () => {
            loadList();
        }
    })
}

// 查询列表
async function loadList() {
    const params = {
        sampleId: state.stdSampleId
    };
    state.isLoading = true;
    const {success, data} = await api.GetStdSampleConcList(params);
    state.isLoading = false;
    if (!success) return 0;
    state.list = data || [];
}

// 点击搜索
function searchClicked() {
    console.primary("搜索条件: ", requestBody)
    reload(true);
}

watch(() => state.stdSampleId, (value) => {
    if (value) {
        loadList()
    }
})

// 设置类型
function setType(row) {
    state.stdSampleId = row.id
    reload(true)
}

onMounted(() => {
})
defineExpose({
    setType
})
</script>

<style lang="scss" scoped>
.ElemList {
    margin: 10px;
    width: calc(100% - 20px);
    box-sizing: border-box;

    .container {
        padding: 10px;
        background: #fff;
        margin-top: 10px;
        box-shadow: 0 0 10px -2px rgba(0, 0, 0, 0.1);
    }
}
</style>
